package com.example.im.ui.adapter

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.im.R
import com.example.im.entity.ChatContent
import com.example.im.entity.ChatRecordEntire
import com.example.im.utils.JsonUtils
import com.example.im.common.Constants
import com.example.im.ui.custom.VoiceTextView
import com.xlkj.ehobe.storage.ui.adapter.holder.RecyclerViewHolder
import kotlinx.android.synthetic.main.linear_item_chat_file.view.*
import kotlinx.android.synthetic.main.linear_item_chat_photo.view.*
import kotlinx.android.synthetic.main.linear_item_chat_text.view.*
import kotlinx.android.synthetic.main.linear_item_chat_title.view.*
import kotlinx.android.synthetic.main.linear_item_chat_voice.view.*
import kotlinx.android.synthetic.main.recycler_item_chat_receive_2.view.*
import kotlinx.android.synthetic.main.recycler_item_chat_send_2.view.*
import org.jivesoftware.smack.packet.Message

class ChatAdapter(
    private val mContext: Context,
    private val mEntire: MutableList<ChatRecordEntire>,
    private val messageType: Int
) :
    RecyclerView.Adapter<RecyclerViewHolder>() {

    private var mOnClickItemListener: OnClickItemListener? = null

    fun setOnClickItemListener(mOnClickItemListener: OnClickItemListener) {
        this.mOnClickItemListener = mOnClickItemListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {

        val entire = this.mEntire[viewType]

        val content = JsonUtils.stringToObject(
            this.mEntire[viewType].body, ChatContent::class.java
        )

        val view = LayoutInflater.from(mContext).inflate(
            when (content.type) {
                Constants.CONTENT_TYPE_VOICE -> R.layout.linear_item_chat_voice
                Constants.CONTENT_TYPE_PHOTO -> R.layout.linear_item_chat_photo
                Constants.CONTENT_TYPE_VIDEO -> R.layout.linear_item_chat_video
                Constants.CONTENT_TYPE_FILE -> R.layout.linear_item_chat_file
                else -> R.layout.linear_item_chat_text
            }, parent, false
        )

        return if (entire.or == Constants.CHAT_SEND) {
            val inflate = LayoutInflater.from(mContext)
                .inflate(R.layout.recycler_item_chat_send_2, parent, false)
            inflate.linear_chat_send_holder.addView(view)
            RecyclerViewHolder(inflate)
        } else {
            val inflate = LayoutInflater.from(mContext)
                .inflate(R.layout.recycler_item_chat_receive_2, parent, false)
            if (messageType == Message.Type.groupchat.ordinal) {
                val titleView = LayoutInflater.from(mContext).inflate(
                    R.layout.linear_item_chat_title, parent, false
                )
                titleView.tv_chat_item_title_name.text = entire.from!!.substring(
                    entire.from!!.indexOf("/") + 1, entire.from!!.length
                )
                inflate.linear_chat_receive_holder.addView(titleView)
            }
            inflate.linear_chat_receive_holder.addView(view)
            RecyclerViewHolder(inflate)
        }
    }

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {


        val view: View = holder.itemView

        val entire = this.mEntire[position]

        val content = JsonUtils.stringToObject(
            entire.body, ChatContent::class.java
        )
        when (content.type) {
            Constants.CONTENT_TYPE_VOICE -> {
                this.setChangeTextView(view.tv_chat_item_voice, entire.or)
                val duration = "${content.fileInfo!!.duration} \""
                view.tv_chat_item_voice.text = duration
                view.tv_chat_item_voice.setOnClickListener { v ->
                    mOnClickItemListener!!.onClickItemContentVoice(v, position)
                }
                view.tv_chat_item_voice.setOnLongClickListener { v ->
                    mOnClickItemListener!!.onClickItemContentLong(v, position)
                    return@setOnLongClickListener true
                }
                if (view.tv_chat_item_voice.isPlaying) {
                    view.tv_chat_item_voice.startAnim()
                } else {
                    view.tv_chat_item_voice.stopAnim()
                }
                view.tv_chat_item_voice.tag = position
            }
            Constants.CONTENT_TYPE_PHOTO -> {
                val url = content.fileInfo!!.netUrl ?: content.fileInfo!!.localUrl
                Glide.with(mContext).load(url).into(view.img_chat_item_photo)
                view.img_chat_item_photo.setOnClickListener { v ->
                    mOnClickItemListener!!.onClickItemContentPhoto(v, position)
                }
                view.img_chat_item_photo.setOnLongClickListener { v ->
                    mOnClickItemListener!!.onClickItemContentLong(v, position)
                    return@setOnLongClickListener true
                }
            }
            Constants.CONTENT_TYPE_VIDEO -> {

            }
            Constants.CONTENT_TYPE_FILE -> {
                view.tv_chat_item_file_name.text = content.fileInfo!!.name
                val fileLength = "${content.fileInfo!!.length} kb"
                view.tv_chat_item_file_length.text = fileLength
                view.cl_chat_item_files.setOnClickListener { v ->
                    mOnClickItemListener!!.onClickItemContentFiles(v, position)
                }
                view.cl_chat_item_files.setOnLongClickListener { v ->
                    mOnClickItemListener!!.onClickItemContentLong(v, position)
                    return@setOnLongClickListener true
                }
            }
            else -> {
                this.setChangeTextView(view.tv_chat_item_text, entire.or)
                view.tv_chat_item_text.text = content.body
                view.tv_chat_item_text.setOnLongClickListener { v ->
                    mOnClickItemListener!!.onClickItemContentLong(v, position)
                    return@setOnLongClickListener true
                }
            }
        }

        if (entire.or == Constants.CHAT_SEND) {
            when (entire.status) {
                Constants.CHAT_STATUS_2 -> {
                    //发送中
                    view.progress_chat_send_load.visibility = View.VISIBLE
                    view.img_chat_send_fail.visibility = View.GONE
                }
                Constants.CHAT_STATUS_3 -> {
                    //发送失败
                    view.progress_chat_send_load.visibility = View.GONE
                    view.img_chat_send_fail.visibility = View.VISIBLE
                    view.img_chat_send_fail.setOnClickListener { v ->
                        mOnClickItemListener!!.onClickItemChatRelay(v, position)
                    }
                }
                else -> {
                    //发送成功
                    view.img_chat_send_fail.visibility = View.GONE
                    view.progress_chat_send_load.visibility = View.GONE
                }
            }

            view.img_chat_send_heard.setOnClickListener { v ->
                mOnClickItemListener!!.onClickItemChatHeard(v, position)
            }
        } else {
            view.img_chat_receive_heard.setOnClickListener { v ->
                mOnClickItemListener!!.onClickItemChatHeard(v, position)
            }
            if (messageType == Message.Type.groupchat.ordinal) {
                view.img_chat_receive_heard.setOnLongClickListener { v ->
                    mOnClickItemListener!!.onClickItemChatLong(v, position)
                    return@setOnLongClickListener true
                }
            } else {
                view.img_chat_receive_heard.setOnLongClickListener(null)
            }
        }

        view.tag = position

    }

    override fun getItemCount(): Int {
        return mEntire.size
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    private fun getItemPosition(id: Int): Int {
        for (index in mEntire.size - 1 downTo 0)
            if (mEntire[index].id == id)
                return index
        return -1
    }

    fun addDataItem(entire: ChatRecordEntire) {
        if (this.mEntire.none { it.id == entire.id }) {
            this.mEntire.add(itemCount, entire)
            this.notifyItemChanged(itemCount)
        } else {
            val position = getItemPosition(entire.id)
            if (position == -1)
                return
            this.mEntire[position] = entire
            this.notifyItemChanged(position)
        }
    }


    fun removeDataItem(index: Int) {
        this.mEntire.removeAt(index)
        this.notifyItemRemoved(index)
        this.notifyItemRangeRemoved(index, itemCount + 1)
    }

    fun clearData() {
        this.mEntire.clear()
        this.notifyDataSetChanged()
    }

    interface OnClickItemListener {

        fun onClickItemChatHeard(v: View, position: Int)
        fun onClickItemChatRelay(v: View, position: Int)
        fun onClickItemChatLong(v: View, position: Int)

        fun onClickItemContentVoice(v: View, position: Int)
        fun onClickItemContentPhoto(v: View, position: Int)
        fun onClickItemContentFiles(v: View, position: Int)

        fun onClickItemContentLong(v: View, position: Int)
    }

    private fun setChangeTextView(view: TextView, or: Int) {
        if (or == Constants.CHAT_SEND) {
            view.setTextColor(Color.WHITE)
            view.setBackgroundResource(R.drawable.style_chat_content_text)
            view.setLinkTextColor(mContext.resources.getColor(R.color.white))
            if (view is VoiceTextView) {
                view.setDrawableRight(R.drawable.animation_voice_right)
            }
        } else {
            view.setTextColor(Color.BLACK)
            view.setBackgroundResource(R.drawable.style_chat_content_text_receive)
            view.setLinkTextColor(mContext.resources.getColor(R.color.blue))
            if (view is VoiceTextView) {
                view.setDrawableLeft(R.drawable.animation_voice_left)
            }
        }
    }

}
